package com.egg.basic.service.dict.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egg.basic.entity.dict.DictData;
import com.egg.basic.mapper.dict.DictDataMapper;
import com.egg.basic.model.request.DictDataPostOrPut;
import com.egg.basic.service.dict.IDictDataService;
import com.egg.common.core.exception.CustomException;
import com.egg.common.mybatis.service.impl.BaseServiceImpl;
import com.egg.common.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author east
 * @since 2022-06-08
 */
@Slf4j
@Service
public class DictDataServiceImpl extends BaseServiceImpl<DictDataMapper, DictData> implements IDictDataService {


    private boolean existDictData(String dictType, String code) {
        return existDictData(dictType, code, Optional.empty());
    }

    private boolean existDictData(String dictType, String code, Optional<Long> filterId) {
        if (ObjectUtil.isEmpty(dictType) || ObjectUtil.isEmpty(code)) {
            return true;
        }
        LambdaQueryWrapper<DictData> qw = buildLambdaQw();
        qw.eq(DictData::getDictTypeCode, dictType);
        qw.eq(DictData::getCode, code);
        filterId.ifPresent(o -> qw.notIn(DictData::getId, o));
        return count(qw) > 0;
    }

    /**
     * 新增
     *
     * @param add
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Long add(DictDataPostOrPut add) {
        DictData dictData = JsonUtil.objToObj(add, DictData.class);
        if (existDictData(dictData.getDictTypeCode(), dictData.getCode())) {
            throw new CustomException("字典数据已存在");
        }
        baseSave(dictData);
        return dictData.getId();
    }

    /**
     * 修改
     *
     * @param id
     * @param edit
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Long edit(String id, DictDataPostOrPut edit) {
        DictData exist = baseGetById(id);
        DictData info = JsonUtil.objToObj(edit, DictData.class);
        if (existDictData(info.getDictTypeCode(), info.getCode(), Optional.of(exist.getId()))) {
            throw new CustomException("字典数据已存在");
        }
        info.setId(exist.getId());
        baseSave(info);
        return info.getId();
    }


    /**
     * @param dictType
     * @return
     */
    @Override
    public List<DictData> findByDictType(String dictType) {
        if (ObjectUtil.isEmpty(dictType)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<DictData> qw = buildLambdaQw();
        qw.orderByDesc(DictData::getSort);
        qw.eq(DictData::getDictTypeCode, dictType);
        return list(qw);
    }

    @Override
    public void delByDictTypeCode(Collection<? extends Serializable> dictTypeCode) {
        if (ObjectUtil.isEmpty(dictTypeCode)) {
            return;
        }
        LambdaQueryWrapper<DictData> qw = buildLambdaQw();
        qw.in(DictData::getDictTypeCode, dictTypeCode);
        remove(qw);
    }


}
